import {reqRegister,reqIdentity,reqLogin,reqUserData,reqLogout} from '@/api/index'
export default {
    namespaced:true,
    state:{ 
        token:'',
        haveonline:false
    },
    getters:{},
    actions:{
        async haveRegister(_,number){
            console.log(number)
            let reslut = await reqRegister(number)
            console.log(reslut)
            if(reslut.code == 200){
                alert('获取成功')
                console.log(reslut.data)
            }else{
                alert('失败了请重试')
            }
        },
        async haveID(_,data){
            let reslut = await reqIdentity(data)
            if(reslut.code == 200){
                alert('恭喜您注册成功')
            }else{
                alert('注册失败请重试')
            }
        },
        async haveLogin({commit},data){
            let result = await reqLogin(data)
            if(result.code == 200){
                alert('登录成功')
                console.log(result)
                localStorage.setItem('token',result.data.token)
                commit('HAVELOGIN',result.data.token)
            }else{
                return Promise.reject('账号或者密码信息错误')
            }
        },
        async haveUserData({commit}){
            let result = await reqUserData()
            if(result.code == 200){
                commit("HAVEUSERDATA",1)
                // commit('HAVEUSERDATA',result.data)
            }else{
                commit("HAVEUSERDATA",0)
                return Promise.reject('err')
            }
        },
        async getOut(){
            let result = await reqLogout()
            console.log(result)
            if(result.code == 200){
                localStorage.removeItem('token')
                return true
            }else{
                return Promise.reject(false)
            }

        }
    },
    mutations:{
        HAVELOGIN(state,value){
            state.token = value
        },
        HAVEUSERDATA(state,value){
            value === 0 ? state.haveonline = false : state.haveonline = true
        }
    }
}